package dbManage;

import java.io.Serializable;

public class LineItem implements Serializable {
	
	public final static int EDIT = 1;
	public final static int INVOICE = 2;
	
	private String sku, description, prodCode, lineText;
	private double width, length, unitCost, totalCost;
	private int quantity, lineNumInd;
	private boolean stock, exact, isItem;
	
	public LineItem(String line, int type){
			stock = false;
			exact = false;
			isItem = false;
			lineText = line;
			if (type == EDIT) {
			try {
				lineNumInd = Integer.parseInt(line.substring(0, 2));
			} catch (Exception e) {
				lineNumInd = 0;
			}
			switch (lineNumInd) {
				case 0: //error or none
					break;
				case 10: //no line
					break;
				case 30: //stock
					prodCode = line.substring(2, 10).trim();
					description = line.substring(10, 30);
				try {
					width = Double.parseDouble(line.substring(30, 35));
					length = Double.parseDouble(line.substring(36, 42));
					quantity = Integer.parseInt(line.substring(64, 68).trim());
					unitCost = Double.parseDouble(line.substring(68, 75));
					totalCost = Double.parseDouble(line.substring(75, 83));
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
					if (line.substring(63, 64).equals("S")) {
						stock = true;
					}
					if (width % 1 != 0) {
						sku = prodCode + (width + 0.1) + "X" + length + "S";
					} else {
						sku = prodCode + width + "X" + length + "S";
					}
					break;
				case 31: //cut down
					prodCode = line.substring(2, 10).trim();
					description = line.substring(10, 30);
				try {
					width = Double.parseDouble(line.substring(30, 35));
					length = Double.parseDouble(line.substring(36, 42));
					quantity = Integer.parseInt(line.substring(64, 68).trim());
					unitCost = Double.parseDouble(line.substring(68, 75));
					totalCost = Double.parseDouble(line.substring(75, 83));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					sku = prodCode + width + "X" + length;
					break;
			}
		}
		
		if(type == INVOICE){
			prodCode = line.substring(0, 6).trim();
			description = line.substring(30, 61).trim();
			if(!prodCode.equalsIgnoreCase("NO")){
				try{
					width = Double.parseDouble(line.substring(19,24));
					length = Double.parseDouble(line.substring(25,30));
					quantity = Integer.parseInt(line.substring(61,65).trim());
					unitCost = Double.parseDouble(line.substring(65, 74).trim());
					totalCost = Double.parseDouble(line.substring(74));
				}
				catch(NumberFormatException e){
					//TODO
				}
				if (width % 1 != 0) {
					sku = prodCode + (width + 0.1) + "X" + length + "S";
				} else {
					sku = prodCode + width + "X" + length + "S";
				}
			}			
		}
	}
	
	public boolean containsPO(){
		if(lineText.contains("PO")){
			return true;
		}
		return false;
	}

}
